package me.nereo.multi_image_selector;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import me.nereo.multi_image_selector.utils.FileUtils;


/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2016/12/19
 * 版本:1.0
 ***/

public class PhotoPickManager {

    /**
     * Open camera
     */
    public static File showCameraAction(Activity context, int requestCode) {
     /*   if(ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
            requestPermission(
                    getString(R.string.mis_permission_rationale_write_storage)
            );
        }else {*/
            File mTmpFile = null;
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                try {
                    mTmpFile = FileUtils.createTmpFile(context);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (mTmpFile != null && mTmpFile.exists()) {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
                    context.startActivityForResult(intent, requestCode);
                } else {
                    Toast.makeText(context, R.string.mis_error_image_not_exist, Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(context, R.string.mis_msg_no_camera, Toast.LENGTH_SHORT).show();
            }
      /*  }*/
      return mTmpFile;
    }
    /**
     * 选照片
     */
    public static void selectPic(Activity context, int requestCode, int count, boolean clipPhoto, int before) {
        MultiImageSelector selector = MultiImageSelector.create(context)
                .showCamera(true) // 是否显示相机. 默认为显示
                .count(1) // 最大选择图片数量, 默认为9. 只有在选择模式为多选时有效
                .multi() // 单选模式, 默认模式;
                .beforeCount(before);
        if (count == 1) {
            selector.single();
        } else {
            selector.multi().count(count);
        }
        selector.isClipPhoto(clipPhoto);
        selector.start(context, requestCode);
    }

    /**
     * 选照片
     */
    public static void selectPicNoCamera(Activity context, int requestCode, int count, boolean clipPhoto, int before) {
        MultiImageSelector selector = MultiImageSelector.create(context)
                .showCamera(false) // 是否显示相机. 默认为显示
                .count(1) // 最大选择图片数量, 默认为9. 只有在选择模式为多选时有效
                .multi() // 单选模式, 默认模式;
                .beforeCount(before);
        if (count == 1) {
            selector.single();
        } else {
            selector.multi().count(count);
        }
        selector.isClipPhoto(clipPhoto);
        selector.start(context, requestCode);
    }

    /**
     * 选照片
     */
    public static void selectPic(Fragment context, int requestCode, int count, boolean clipPhoto, int before) {
        MultiImageSelector selector = MultiImageSelector.create(context.getActivity())
                .showCamera(true) // 是否显示相机. 默认为显示
                .count(1) // 最大选择图片数量, 默认为9. 只有在选择模式为多选时有效
                .multi() // 单选模式, 默认模式;
                .beforeCount(before) // 默认已选择图片. 只有在选择模式为多选时有效
                ;
        if (count == 1) {
            selector.single();
        } else {
            selector.multi().count(count);
        }
//        if (datas!=null && datas.size()>0){
//            selector.origin(datas);
//        }
        selector.isClipPhoto(clipPhoto);
        selector.start(context, requestCode);
    }

    /**
     * 选照片
     */
    public static void selectPicNoCamera(Fragment context, int requestCode, int count, boolean clipPhoto, int before) {
        MultiImageSelector selector = MultiImageSelector.create(context.getActivity())
                .showCamera(false) // 是否显示相机. 默认为显示
                .count(1) // 最大选择图片数量, 默认为9. 只有在选择模式为多选时有效
                .multi() // 单选模式, 默认模式;
                .beforeCount(before) // 默认已选择图片. 只有在选择模式为多选时有效
                ;
        if (count == 1) {
            selector.single();
        } else {
            selector.multi().count(count);
        }
//        if (datas!=null && datas.size()>0){
//            selector.origin(datas);
//        }
        selector.isClipPhoto(clipPhoto);
        selector.start(context, requestCode);
    }


    /**
     * 选照片
     */
    public static void selectPic(Activity context, int requestCode, boolean clipPhoto, int clipW, int clipH) {
        MultiImageSelector selector = MultiImageSelector.create(context)
                .showCamera(true) // 是否显示相机. 默认为显示
                .clipWidth(clipW)
                .clipHeight(clipH);
        selector.single();
        selector.isClipPhoto(clipPhoto);
        selector.start(context, requestCode);
    }

    public static void selectPicnoCamera(Activity context, int requestCode, boolean clipPhoto, int clipW, int clipH) {
        MultiImageSelector selector = MultiImageSelector.create(context)
                .showCamera(false) // 是否显示相机. 默认为显示
                .clipWidth(clipW)
                .clipHeight(clipH);
        selector.single();
        selector.isClipPhoto(clipPhoto);
        selector.start(context, requestCode);
    }

    /**
     * 选照片
     */

    public static void selectPic(Fragment context, int requestCode, boolean clipPhoto, int clipW, int clipH) {
        MultiImageSelector selector = MultiImageSelector.create(context.getContext())
                .showCamera(true) // 是否显示相机. 默认为显示
                .clipWidth(clipW)
                .clipHeight(clipH);
        selector.single();
        selector.isClipPhoto(clipPhoto);
        selector.start(context, requestCode);
    }


    public static ArrayList<String> getPhoto(Intent data) {
        if (data != null && data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT) != null){
            ArrayList imagePaths = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            return imagePaths;
        }
            return null;
    }
}
